package easy.加一;

import java.util.Arrays;

public class Solution {
    public int[] plusOne(int[] digits) {
        int length = digits.length;
        for (int i = length - 1; i >= 0; i--) {
            if (digits[i] != 9) {
                digits[i]++;
                for (int j = i + 1; j <= length - 1; j++) {
                    digits[j] = 0;
                }
                return digits;
            }
        }
        //如果全是9
        int[] digitsNew = new int[length + 1];
        digitsNew[0] = 1;
        return digitsNew;
    }

    public static void main(String[] args) {
        int[] digits = {1, 2, 3};
        int[] digits2 = {4, 3, 9, 1, 9, 9};
        int[] digits3 = {9, 9, 9};
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.plusOne(digits)));
        System.out.println(Arrays.toString(solution.plusOne(digits2)));
        System.out.println(Arrays.toString(solution.plusOne(digits3)));
    }
}
